# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields
from openerp.osv import osv


class shop(osv.osv):
    _inherit = 'shop'

    _cache_user = {}

    TYPE_SELECTION = [
        ('integral', u'积分商城'),
        ('enterprise', u'企业商城'),
    ]

    def check_user_access(self, cr, uid, ids, user_id, context=None):
        if self._cache_user.get(user_id):
            return True

        for shop in self.browse(cr, uid, ids, context=context):
            for user in shop.user_ids:
                if user.id == user_id:
                    self.__class__._cache_user[user_id] = True

                    return True

        return False

    _columns = {
        'code': fields.char(u'编号', select=True),
        'line_ids': fields.one2many('shop.line', 'shop_id', u'上架产品'),
        'type': fields.selection(TYPE_SELECTION, u'类型'),
        'redeem_ratio': fields.integer(u'积分兑换比'),
        'user_ids': fields.many2many('res.users', string=u'商城用户'),
        'categ_ids': fields.many2many('product.public.category', string=u'商城分类'),
        'article_categ_ids': fields.many2many('mshop.article.category', string=u'文章分类'),
    }

    _sql_constraints = [
        ('uniqu_shop_code', 'UNIQUE(code)', u'商城编号必须唯一')
    ]

    _default = {
        'type': 'enterprise',
        'redeem_ratio': 500,
    }


class shop_line(osv.osv):
    _name = 'shop.line'
    _order = 'is_online desc, sequence desc, id'

    def do_online(self, cr, uid, ids, context=None):
        return self.write(cr, uid, ids, {'is_online': True}, context=context)

    def do_offline(self, cr, uid, ids, context=None):
        return self.write(cr, uid, ids, {'is_online': False}, context=context)

    _columns = {
        'shop_id': fields.many2one('shop', u'商城', required=True, select=True),
        'product_id': fields.many2one('product.template', u'产品'),
        'name': fields.char(u'产品名称'),
        'sequence': fields.char(u'顺序'),
        'is_online': fields.boolean(u'是否上架'),
    }

    _default = {
        'sequence': 0,
    }

    _sql_constraints = [
        ('unique_product_id', 'UNIQUE(shop_id, product_id)', u'每个产品只能选择一次')
    ]


class product_template(osv.osv):
    _inherit = 'product.template'

    _columns = {
        'shop_line_ids': fields.one2many('shop.line', 'product_id', '商城商品明细行'),
    }
